package com.dianrui.agent.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.TextView;

import com.dianrui.agent.Constant;
import com.dianrui.agent.R;
import com.dianrui.agent.update.DefaultUpdateParser;
import com.dianrui.agent.util.MyUtil;
import com.dianrui.agent.util.SharedUtil;
import com.dianrui.agent.util.Url;
import com.xuexiang.xupdate.XUpdate;

import butterknife.BindView;
import butterknife.OnClick;

public class SettingActivity extends BaseActivity {


    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.ver)
    TextView ver;

    @Override
    public int getContentViewId() {
        return R.layout.activity_setting;
    }

    @Override
    public void initView() {
        title.setText("设置");
        ver.setText("版本:" + MyUtil.getAppVersionName(SettingActivity.this));
    }


    @OnClick({R.id.back, R.id.login_out, R.id.account_security_layout, R.id.about_layout, R.id.service_layout, R.id.update_layout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.login_out:
                spUtils.clear();
                JumpLoginActivitys(LoginActivity.class);
                break;
            case R.id.account_security_layout:
                JumpActivitys(AccountSecurityActivity.class);
                break;

            case R.id.about_layout:
                JumpActivitys(WebViewActivity.class);
                break;

            case R.id.service_layout:
                callPhone("4006801121");
                break;

            case R.id.update_layout:
                XUpdate.newBuild(SettingActivity.this).updateUrl(Constant.UPDATEURL + MyUtil.getVersionName(SettingActivity.this) + "/apply_type/1").updateParser(new DefaultUpdateParser()).update();
                break;

            default:
                break;
        }
    }


    public void callPhone(String phoneNum) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(SettingActivity.this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNum));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            } else {
                ActivityCompat.requestPermissions(SettingActivity.this, new String[]{Manifest.permission.CALL_PHONE}, 1);
            }
        } else {
            Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNum));
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }

    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case 1:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    callPhone("4006801121");
                }
                break;
            default:
                break;

        }

    }
}
